package com.atguigu.ssyx.acl.service.Impl;

import com.atguigu.ssyx.acl.mapper.AdminRoleMapper;
import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdminRoleService adminRoleService;

    /**
     * 分页查询角色信息。
     * 本方法通过接收分页参数和角色查询条件，调用底层Mapper进行分页查询，返回查询结果的分页对象。
     * 主要用于角色管理模块的角色列表展示，支持通过角色名进行查询。
     *
     * @param pageParam   分页参数对象，包含当前页码和每页条数等信息。
     * @param roleQueryVo 角色查询条件对象，封装了角色名等查询条件。
     * @return 返回分页查询结果，包含查询到的角色列表和分页信息。
     */
    @Override
    public IPage<Role> selectRolePage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {
        String roleName = roleQueryVo.getRoleName();

        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
//        不为空
        if (!roleName.isEmpty()) {
//            roleName有值的时候进入
//            不适用eq,用like.  where      RoleName like  ?
            wrapper.like(Role::getRoleName, roleName);
        }
        IPage<Role> rolePage = roleMapper.selectPage(pageParam, wrapper);
        return rolePage;
    }


    /**
     * 根据管理员id查询得出角色id集合,之后拿到role表中查出对应的记录.
     *
     * @param adminId
     * @return
     */
    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {

//        1.查询所有角色
        List<Role> allRolesList = baseMapper.selectList(null);

        List<AdminRole> existUserRoleList = adminRoleService.list(new QueryWrapper<AdminRole>().eq("admin_id", adminId).select("role_id"));

//       拿出里面的角色id   4
        List<Long> existRoleList = existUserRoleList.stream().map(c -> c.getRoleId()).collect(Collectors.toList());

        //对角色进行分类
        List<Role> assignRoles = new ArrayList<Role>();

//        所有角色表allRolesList
//        集合里面的方法contains
        for (Role role : allRolesList) {
            //已分配
            if (existRoleList.contains(role.getId())) {
                assignRoles.add(role);
            }
        }
//返回的是角色表集合和自己选择的角色集合.
        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("assignRoles", assignRoles);
        roleMap.put("allRolesList", allRolesList);
        return roleMap;
    }




    @Override
    public void saveUserRoleRealtionShip(Long adminId, Long[] roleIds) {
//       1. 删除原有数据.
        LambdaQueryWrapper<AdminRole> adminRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        adminRoleLambdaQueryWrapper.eq(AdminRole::getAdminId, adminId);
        adminRoleService.remove(adminRoleLambdaQueryWrapper);

        ArrayList<AdminRole> adminRoleList = new ArrayList<>();

//      2. 添加新的数据.
        for (Long roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);
            adminRoleList.add(adminRole);
        }
        adminRoleService.saveBatch(adminRoleList);
    }

}